package com.shengdou.as.slopsapptest.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.bean.FailureListBean;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.DateUtils;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;
import com.shengdou.as.slopsapptest.utils.SharedPreferenceUtil;
import com.shengdou.as.slopsapptest.utils.XListViewUtils.XListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 故障列表
 */
public class FailureListActivity extends BaseActivity implements XListView.IXListViewListener {

    private String phone;
    private Map<String, String> params = new HashMap<>();
    private ArrayList<FailureListBean.ListsBean> array = new ArrayList<>();
    private XListView mListView;
    private String time;
    private LinearLayout gone;
    private int page;
    private MyAdapter adapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.FAILURELIST:
                    String result = msg.obj.toString();
                    LogUtil.e("sult", result);
                    FailureListBean failure = new Gson().fromJson(result, FailureListBean.class);
                    if (failure.getLists() != null) {
                        array.addAll(failure.getLists());
                    }
                    mListView.stopLoadMore();
                    mListView.stopRefresh();


                    if (array.size() == 0) {
                        adapter.notifyDataSetChanged();
                        mListView.setVisibility(View.GONE);
                        gone.setVisibility(View.VISIBLE);
                    } else {
                        mListView.setVisibility(View.VISIBLE);
                        gone.setVisibility(View.GONE);

                        if (page == failure.getPage_num()) {
//                        ToastUtils.showToast("没有更多数据了!");
                            mListView.setPullLoadEnable(false);
                        } else {
                            mListView.setPullLoadEnable(true);
                        }
                    }

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_failure_list);
        init();
    }

    private void init() {
        phone = SharedPreferenceUtil.getPreferenceString(FailureListActivity.this, "mobile");
        mListView = (XListView) findViewById(R.id.listview);
        adapter = new MyAdapter(array);
        gone = (LinearLayout) findViewById(R.id.txt_gone);
        page = 1;
        //故障列表
        params.put("page", page + "");
        params.put("mobile", phone + "");
        HttpUtils.postFactory(mHandler, NetConstant.FAILURELIST, params, Constants.FAILURELIST, FailureListActivity.this);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        time = df.format(new Date());
        mListView.setPullLoadEnable(false);
        mListView.setXListViewListener(this);
        mListView.setRefreshTime(time);
        mListView.setAdapter(adapter);
    }

    public void onTabClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        array.clear();
        //故障列表
        params.put("page", page + "");
        params.put("mobile", phone + "");
        mListView.setPullLoadEnable(true);
        HttpUtils.postFactory(mHandler, NetConstant.FAILURELIST, params, Constants.FAILURELIST, FailureListActivity.this);
    }

    @Override
    public void onLoadMore() {
        page++;
        //故障列表
        params.put("page", page + "");
        params.put("mobile", phone + "");
        HttpUtils.postFactory(mHandler, NetConstant.FAILURELIST, params, Constants.FAILURELIST, FailureListActivity.this);
    }

    private class MyAdapter extends BaseAdapter {
        ArrayList<FailureListBean.ListsBean> mArray;

        public MyAdapter(ArrayList<FailureListBean.ListsBean> array) {
            this.mArray = array;
        }

        @Override
        public int getCount() {
            return mArray.size();
        }

        @Override
        public Object getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(FailureListActivity.this, R.layout.item_failure_list, null);
                holder.mTxtTitle = (TextView) convertView.findViewById(R.id.txt_failure_title);
                holder.mTxtDate = (TextView) convertView.findViewById(R.id.txt_failure_date);
                holder.mTxtName = (TextView) convertView.findViewById(R.id.txt_failure_name);
                holder.mTxtInfo = (TextView) convertView.findViewById(R.id.txt_failure_info);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtTitle.setText(mArray.get(position).getPool_name() + "故障信息");
            holder.mTxtDate.setText(DateUtils.stringFormateMin(mArray.get(position).getCreate_time()));
            holder.mTxtName.setText(mArray.get(position).getStatus_text());
            holder.mTxtInfo.setText(mArray.get(position).getInfo());
            return convertView;
        }
    }

    class ViewHolder {
        TextView mTxtTitle;
        TextView mTxtDate;
        TextView mTxtName;
        TextView mTxtInfo;
    }
}
